package controller;

import service.UserService;
import service.impl.UserServiceimpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//接收页面传递过来的数据，将数据传递回页面，页面跳转
//需要调用Service方法
@WebServlet(urlPatterns = "/login")
public class loginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("username");
        String password = req.getParameter("userpassword");

        UserService userService = new UserServiceimpl();
        try {

            boolean isLogin = userService.login(name, password);

            if (isLogin) {
//                resp.sendRedirect("/shiyan1/stu?action=list");
                resp.sendRedirect("/shiyan1/book?action=list");
            }else {
                resp.sendRedirect("/shiyan1/login.jsp");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
